/*
 * Copyright (c) 2017 Qunar.com. All Rights Reserved.
 */
package com.guoyu.project.web.api;

import javax.annotation.Resource;

import com.google.common.collect.Maps;
import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.model.Article;
import com.guoyu.project.model.enums.ArticleType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.guoyu.project.common.web.APIResponse;
import com.guoyu.project.service.ArticleService;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

/**
 * @author: libo Date: 17-6-30 Time: 上午11:13
 * @version: \$Id$
 */
@Controller
@RequestMapping("api/search")
public class SearchApiController extends BaseApiController {

    @Resource
    private ArticleService articleService;

    @RequestMapping("all")
    @ResponseBody
    public APIResponse all(@RequestParam(defaultValue = "0") int offset, @RequestParam(defaultValue = "10") int limit,
            String keyword) {
        if (StringUtils.isBlank(keyword)) {
            return APIResponse.returnFail("请输入关键字");
        }

        Map<String, Object> params = Maps.newHashMap();
        params.put("keyword", keyword);
        params.put("notMinorType", ArticleType.PROGRESS);
        Pagination<Article> pagination = articleService.queryArticles(offset, limit, params);
        return APIResponse.returnSuccess(pagination);
    }

    @RequestMapping("project")
    @ResponseBody
    public APIResponse project(@RequestParam(defaultValue = "0") int offset,
            @RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "0") int provinceId,
            @RequestParam(defaultValue = "0") int schoolId, String[] types, String projectName, Date startTime,
            Date endTime) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("majorType", ArticleType.PROJECT);
        params.put("keyword", projectName);
        if (provinceId > 0) {
            params.put("provinceId", provinceId);
        }
        if (schoolId > 0) {
            params.put("schoolId", schoolId);
        }
        if (startTime != null) {
            params.put("startTime", startTime);
        }
        if (endTime != null) {
            params.put("endTime", endTime);
        }
        if (types != null && types.length > 0) {
            params.put("minorTypes", types);
        }

        Pagination<Article> pagination = articleService.queryArticles(offset, limit, params);
        return APIResponse.returnSuccess(pagination);
    }
}
